package com.hongxin.nw.netty;

import com.hongxin.nw.netty.config.NettyServer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import java.net.InetSocketAddress;
/**
 * @author guoxiaolin
 * @date 创建于 上午9:14 2019/5/10
 * @Descibe
 */
@SpringBootApplication(scanBasePackages = {"com.hongxin.nw.netty"},exclude = {DataSourceAutoConfiguration.class})
public class NettyApplication  implements CommandLineRunner {


    @Value("${n.url}")
    private String url;
    /**
     * 并发链接缓冲数量
     */
    @Value("${n.backlog}")
    private int backlog;
    /**
     * 监听端口
     */
    @Value("${n.port}")
    private int port;
    /**
     * 全局写流量上限，byte
     */
    @Value("${n.writeGlobalLimit}")
    private long writeGlobalLimit;
    /**
     * 全局读流量上限，byte
     */
    @Value("${n.readGlobalLimit}")
    private long readGlobalLimit;
    /**
     * 单通道写上限，byte
     */
    @Value("${n.writeChannelLimit}")
    private long writeChannelLimit;
    /**
     * 单通道读上限，byte
     */
    @Value("${n.readChannelLimit}")
    private long readChannelLimit;
    /**
     * 流量测算间隔，ms
     */
    @Value("${n.checkInterval}")
    private long checkInterval;
    /**
     * 暂停读写最长时间，ms
     */
    @Value("${n.maxTime}")
    private long maxTime;
    /**
     * 读超时，s
     */
    @Value("${n.readingTimeout}")
    private int readingTimeout;

    /**
     * 下发指令超时,ms
     */
    @Value("${n.responseTimeout}")
    private long responseTimeout;

    @Autowired
    private NettyServer socketServer;

    public static void main(String[] args) {
        SpringApplication.run(NettyApplication.class, args);
    }

    @Override
    public void run(String... strings) {
        InetSocketAddress address = new InetSocketAddress(url, port);
        ChannelFuture future = socketServer.run(address);
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                socketServer.destroy();
            }
        });
        future.channel().closeFuture().syncUninterruptibly();
    }

}
